package com.atguigu.yygh.hosp.testmongo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.runtime.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "MongoRepository操作")
@RestController
@RequestMapping("mongoRepository")
public class MongoRepositoryController {
    @Autowired
    private UserRepository userRepository;

    @GetMapping("method1")
    protected  void method1(){
        List<User> list = userRepository.getByNameLike("琳");
        System.out.println(list);
    }

    @GetMapping("testMethod1")
    public void testMethod1(){
        List<User> list = userRepository.findByNameAndAge("wenlin",22);
        System.out.println(list);
    }

    @ApiOperation("添加")
    @GetMapping("create")
    public void create() {
        User user = new User();
        user.setAge(30);
        user.setName("李四");
        user.setEmail("3332200@qq.com");
        User u = userRepository.save(user);
        System.out.println("u = " + u);
    }

    //添加修改都用save 有id做修改 无id做添加
    @ApiOperation("修改")
    @GetMapping("update")
    public void update() {
        //注意需要get()才是真正想要的数据
        User user = userRepository.findById("6130b6489c64ec16a4459ba2").get();
        user.setAge(100);
        user.setName("造林");
        user.setEmail("11111122222@qq.com");
        userRepository.save(user);
    }

    //查询所有
    @ApiOperation("查询所有")
    @GetMapping("findAll")
    public void findUser() {
        List<User> userList = userRepository.findAll();
        System.out.println(userList);
    }

    //写法复杂的条件查询
    @ApiOperation("条件查询")
    @GetMapping("findQuery")
    public void findQuery() {
        User user = new User();
        user.setName("zhangsan");
        user.setAge(23);
        Example<User> userExample = Example.of(user);
        List<User> userList = userRepository.findAll(userExample);
        System.out.println("list = " + userList);
    }

    //写法复杂的模糊查询
    @ApiOperation("模糊查询")
    @GetMapping("findLike")
    public void findLike() {
        //设置匹配规则:模糊查询
        //创建匹配器，即如何使用查询条件
        ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改变默认字符串匹配方式：模糊查询
                .withIgnoreCase(true); //改变默认大小写忽略方式：忽略大小写
        User user = new User();
        user.setName("琳");
        Example<User> example = Example.of(user, matcher);
        List<User> userList = userRepository.findAll(example);
        System.out.println("userList = " + userList);
    }

    //分页查询
    @ApiOperation("分页查询")
    @GetMapping("findPage")
    public void findPage() {
        Sort sort = Sort.by(Sort.Direction.DESC, "ages");
        //当前页(0代表第一页),每页记录数,排序规则
        PageRequest pageable = PageRequest.of(0, 2, sort);
        ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改变默认字符串匹配方式：模糊查询
                .withIgnoreCase(true); //改变默认大小写忽略方式：忽略大小写
        User user = new User();
        user.setName("wenlin");
        Example<User> example = Example.of(user, matcher);
        Page<User> page = userRepository.findAll(example, pageable);
        long totalElements = page.getTotalElements();
        int totalPages = page.getTotalPages();
        List<User> content = page.getContent();
        System.out.println("content = " + content);
        System.out.println("totalPages = " + totalPages);
        System.out.println("totalElements = " + totalElements);
        System.out.println(page);
    }

    //删除
    @ApiOperation("删除")
    @GetMapping("delete")
    public void delete() {
        userRepository.deleteById("6130cc12f93d80413408e0c5");
    }


}
